package com.yjd.bi.statistic.control;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yjd.bi.promotion.model.SpreadRelationModel;
import com.yjd.bi.promotion.service.ISpreadRelationService;
import com.yjd.bi.user.dto.UserRankDto;
import com.yjd.bi.user.service.IUserService;
import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.model.PagerHtmlModel;
import com.yjd.comm.enums.UserEnums.UserType;
import com.yjd.comm.gift.model.GiftRegisterBindModel;
import com.yjd.comm.gift.service.IGiftRegisterBindService;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.PagerUtil;
import com.yjd.comm.util.ServletUtil;
@Controller
@Scope("prototype")
@RequestMapping("/rank")
public class RankControl extends BaseControl {
	@ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
	private IUserService userService;
	
	@ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
	private ISpreadRelationService spreadRelationService;
	
	@ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
	private IGiftRegisterBindService giftRegisterBindService;
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/index")
	public String index()throws Exception{
		
		
		
		
		
		//获取昨天的日期
		Date dt=new Date(new Date().getTime()-24*60*60*1000);
	    SimpleDateFormat matter1=new SimpleDateFormat("yyyy-MM-dd");
	    request.setAttribute("today", matter1.format(dt));
	    
	    Map maps = ServletUtil.getPageParameterMap(request);
	    maps.put("rows", 100);
	    
	    int type = 1;
	    long min = maps.get("minLimit")==null?1:Integer.parseInt(maps.get("minLimit").toString());
	    long max = maps.get("maxLimit")==null?1000:Integer.parseInt(maps.get("maxLimit").toString()); 
	    if(max<min){
	    	return "rank.index";
	    }
	    if(maps.get("type")==null){
	    	maps.put("type", 1);
	    }
	    if(maps.get("endTime")==null||"".equals(maps.get("endTime"))){
	    	maps.put("endTime", DateUtil.dateStringToTimestamp(matter1.format(dt), DateUtil.PATTERN_yyyy_MM_dd)+86399);
	    }else{
	    	maps.put("endTime", DateUtil.dateStringToTimestamp(maps.get("endTime").toString(), DateUtil.PATTERN_yyyy_MM_dd)+86399);
	    }
	    maps.put("minLimit", min-1);
	    maps.put("maxLimit", max-min+1);
	    
	    Map temp = FrameUtil.newHashMap("endTime",maps.get("endTime"));
	    //查询所有的待回收金额的用户信息
	    List<UserRankDto> borrowPriceUsers = userService.getRankUsers(1, temp);
	    Map<String,UserRankDto> borrowPriceUserMap = FrameUtil.toMap(borrowPriceUsers, "user_id");
	    //查询所有的投资金额的用户信息
	    List<UserRankDto> investPriceUsers = userService.getRankUsers(2, temp);
	    Map<String,UserRankDto> investPriceUsersMap = FrameUtil.toMap(investPriceUsers, "user_id");
	    //查询所有的充值金额的用户信息
	    List<UserRankDto> rechangePriceUsers = userService.getRankUsers(3, temp);
	    Map<String,UserRankDto> rechangePriceUsersMap = FrameUtil.toMap(rechangePriceUsers, "user_id");
	    //查询所有体现金额的用户信息
	    List<UserRankDto> cashPriceUsers = userService.getRankUsers(4, temp);
	    Map<String,UserRankDto> cashPriceUsersMap = FrameUtil.toMap(cashPriceUsers, "user_id");

	    
	    
	    PagerHtmlModel pager = null;
	    //如果是按照待回收金额排序
	    if(Integer.parseInt(maps.get("type").toString())==1){
	    	pager = PagerUtil.getPagerHtmlModel(userService.getPageModel(maps, "getBorrowPriceUser", DbWREnums.READ),ServletUtil.getPageParameterMap(request),
					ServletUtil.getUrl("/rank/index"));
	    	
	    	List<UserRankDto> list = pager.getPagerModel().getDataList();
	    	
	    	for (int i = 0; i < pager.getPagerModel().getDataList().size(); i++) {
				UserRankDto user = (UserRankDto) pager.getPagerModel().getDataList().get(i);
				user.setInvest_price(investPriceUsersMap.get(user.getUser_id())==null?0:investPriceUsersMap.get(user.getUser_id()).getInvest_price());
				user.setRechange_price(rechangePriceUsersMap.get(user.getUser_id())==null?0:rechangePriceUsersMap.get(user.getUser_id()).getRechange_price());
				user.setCash_price(cashPriceUsersMap.get(user.getUser_id())==null?0:cashPriceUsersMap.get(user.getUser_id()).getCash_price());
			}
	    	
	    }else if(Integer.parseInt(maps.get("type").toString())==2){//按照投资金额排序
	    	pager = PagerUtil.getPagerHtmlModel(userService.getPageModel(maps, "getInvestPriceUser", DbWREnums.READ),ServletUtil.getPageParameterMap(request),
					ServletUtil.getUrl("/rank/index"));
	    	
	    	List<UserRankDto> list = pager.getPagerModel().getDataList();
	    	
	    	for (int i = 0; i < pager.getPagerModel().getDataList().size(); i++) {
				UserRankDto user = (UserRankDto) pager.getPagerModel().getDataList().get(i);
				user.setBorrow_price(borrowPriceUserMap.get(user.getUser_id())==null?0:borrowPriceUserMap.get(user.getUser_id()).getBorrow_price());
				//user.setInvest_price(investPriceUsersMap.get(user.getUser_id())==null?0:investPriceUsersMap.get(user.getUser_id()).getInvest_price());
				user.setRechange_price(rechangePriceUsersMap.get(user.getUser_id())==null?0:rechangePriceUsersMap.get(user.getUser_id()).getRechange_price());
				user.setCash_price(cashPriceUsersMap.get(user.getUser_id())==null?0:cashPriceUsersMap.get(user.getUser_id()).getCash_price());
			}
	    }else if(Integer.parseInt(maps.get("type").toString())==3){//按照充值金额排序
	    	pager = PagerUtil.getPagerHtmlModel(userService.getPageModel(maps, "getRechangePriceUser", DbWREnums.READ),ServletUtil.getPageParameterMap(request),
					ServletUtil.getUrl("/rank/index"));
	    	
	    	List<UserRankDto> list = pager.getPagerModel().getDataList();
	    	
	    	for (int i = 0; i < pager.getPagerModel().getDataList().size(); i++) {
				UserRankDto user = (UserRankDto) pager.getPagerModel().getDataList().get(i);
				user.setBorrow_price(borrowPriceUserMap.get(user.getUser_id())==null?0:borrowPriceUserMap.get(user.getUser_id()).getBorrow_price());
				user.setInvest_price(investPriceUsersMap.get(user.getUser_id())==null?0:investPriceUsersMap.get(user.getUser_id()).getInvest_price());
				//user.setRechange_price(rechangePriceUsersMap.get(user.getUser_id())==null?0:rechangePriceUsersMap.get(user.getUser_id()).getRechange_price());
				user.setCash_price(cashPriceUsersMap.get(user.getUser_id())==null?0:cashPriceUsersMap.get(user.getUser_id()).getCash_price());
			}
	    }else if(Integer.parseInt(maps.get("type").toString())==4){//按照体现金额排序
	    	pager = PagerUtil.getPagerHtmlModel(userService.getPageModel(maps, "getCashPriceUser", DbWREnums.READ),ServletUtil.getPageParameterMap(request),
					ServletUtil.getUrl("/rank/index"));
	    	
	    	List<UserRankDto> list = pager.getPagerModel().getDataList();
	    	
	    	for (int i = 0; i < pager.getPagerModel().getDataList().size(); i++) {
				UserRankDto user = (UserRankDto) pager.getPagerModel().getDataList().get(i);
				user.setBorrow_price(borrowPriceUserMap.get(user.getUser_id())==null?0:borrowPriceUserMap.get(user.getUser_id()).getBorrow_price());
				user.setInvest_price(investPriceUsersMap.get(user.getUser_id())==null?0:investPriceUsersMap.get(user.getUser_id()).getInvest_price());
				user.setRechange_price(rechangePriceUsersMap.get(user.getUser_id())==null?0:rechangePriceUsersMap.get(user.getUser_id()).getRechange_price());
				//user.setCash_price(cashPriceUsersMap.get(user.getUser_id())==null?0:cashPriceUsersMap.get(user.getUser_id()).getCash_price());
			}
	    }
	    
	    
	    List<UserRankDto> list = pager.getPagerModel().getDataList();
	    List user_ids = FrameUtil.getBeanFieldValues(list, "user_id");
	    List<SpreadRelationModel> spreads = spreadRelationService.getModelList(FrameUtil.newHashMap("idList",user_ids), DbWREnums.READ);
	    
	    Map spreaders = FrameUtil.toMap(spreads, "spreader");//推广人
	    Map registers = FrameUtil.toMap(spreads, "register");//被推广用户
	    List<GiftRegisterBindModel> gifts = giftRegisterBindService.getModelList(FrameUtil.newHashMap("idList",user_ids), DbWREnums.READ);
	    Map users = FrameUtil.toMap(gifts, "user_id");//推广人
	    Map shareusers = FrameUtil.toMap(gifts, "share_user_id");//被推广用户
	    
	    
	    for (UserRankDto user : list) {
	    	Map strMap = FrameUtil.newHashMap();
	    	Integer user_id = user.getUser_id().intValue();
	    	if(shareusers.containsKey(user_id) || registers.containsKey(user_id)){
	    		strMap.put("shareuser", true);
	    	}
	    	if(users.containsKey(user_id)|| spreaders.containsKey(user_id)){
	    		strMap.put("user",true);
	    	}
			if((user.getUser_type() & UserType.DZ_USER.getCode()) == UserType.DZ_USER.getCode()){
				strMap.put("dz",true);
			}
			if((user.getUser_type() & UserType.BORROWER.getCode()) == UserType.BORROWER.getCode()){
				strMap.put("jk",true);
			}
			user.setStrMap(strMap);
		}
	    
	    
		
	    request.setAttribute("pager", pager);
		return "rank.index";
	}

}
